package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.query.TravelQuery;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.security.auth.login.LoginException;

@Controller
@RequestMapping("/travel")
public class TravelController {
    @Autowired
    private ITravelService travelService;

    @Autowired
    private IDestinationService destinationService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo")TravelQuery qo){
        Page<Travel> page = travelService.query(qo);
        model.addAttribute("page",page);
        return "travel/list";
    }


    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Travel travel){
        travelService.saveOrUpdate(travel);
        return JsonResult.success();
    }


    @RequestMapping("/input")
    public String input(Model model,String id){
        if(StringUtils.hasLength(id)){
            //travel
            //travelService.get(id):解决页面编辑的回显
            model.addAttribute("travel",travelService.get(id));

        }

        //<2>catalogs 把数据共享出去,查询是攻略下拉框,所以使用travelCatalogService

        return "travel/input" ;
    }


    @RequestMapping("/changeState")
    @ResponseBody
    public Object changeState(String id,int state){
        return JsonResult.success();
    }

    //审核
    @RequestMapping("/audit")
    @ResponseBody
    public Object audit(String id,int state) {
        travelService.audit(id,state);
        return JsonResult.success();
    }

    //查看
    @RequestMapping("/getContentById")
    @ResponseBody
    public Object getContentById(String id) {
        //在这里通过用户的id获取用户对象存储到travel对象中
        Travel travel = travelService.get(id);
        return JsonResult.success(travel.getContent());//我们只需要拿到游记的内容即可
    }
}
